package cn.math.appmath.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.math.appmath.bean.PageBean;
import cn.math.appmath.bean.ProjectBean;
import cn.math.appmath.dao.ProjectDao;
import cn.math.appmath.dao.SomeConstants;
import cn.math.appmath.entity.Project;

public class ProjectDaoImpl extends HibernateDaoSupport implements ProjectDao {
	private static final Log log = LogFactory.getLog(ProjectDaoImpl.class);

	public ProjectBean queryProjects(int page, String sqlStr, ArrayList<Object> objectList) {
		log.debug("query projects by list");
		try{
			String hql = "";
			String hid = "";
			Query query;
			List<Project> list;
			StringBuffer sb_hql = new StringBuffer();
			if(!sqlStr.equals("") && sqlStr!=null){
				sb_hql.append(sqlStr);
				hid = "none";
				hql = "from Project" + sb_hql.toString();
				query = getSession().createQuery(hql);
				for(int i=0; i<objectList.size(); i++){
					query.setParameter(i, objectList.get(i));
				}
				list = (List<Project>)query.list();
				return new ProjectBean(0, 0, list, hid, 0);
			}else{
				hql = "select count(*) from Project";
				query = getSession().createQuery(hql);
				int allRow = Integer.parseInt(String.valueOf(query.uniqueResult()));
				int totalPage = PageBean.countTotalPage(SomeConstants.TableSize, allRow);			
				int currentPgae;
				if(page == totalPage)
					currentPgae = totalPage;
				else
					currentPgae = PageBean.countCurrentPage(page);
				int currentRow = PageBean.countCurrentRow(SomeConstants.TableSize, currentPgae);
				hql = "from Project";
				query = getSession().createQuery(hql);
				query.setFirstResult(currentRow);
				query.setMaxResults(SomeConstants.TableSize);
				if(allRow != 0){
					list = (List<Project>)query.list();
				}else{
					list = null;
				}
				return new ProjectBean(totalPage, currentPgae, list, hid, allRow);
			}
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public void save(Project transientInstance) {
		log.debug("saving Project instance");
		try {
			Session session = getSession();
			Transaction tx = session.beginTransaction();
			session.saveOrUpdate(transientInstance);
			tx.commit();
			session.close();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public boolean batchDelete(Integer[] ids) {
		log.debug("batchDelete Project instance");
		try{
			String hql = "delete from Project where pno in (:ids)";
			Query query = getSession().createQuery(hql);
			query.setParameterList("ids", ids);
			query.executeUpdate();
			log.debug("batchDelete successful");
			return true;
		}catch (Exception ex){
			log.error("batchDelete failed", ex);
			ex.printStackTrace();
			return false;
		}
	}
	
	public void delete(Project persistentInstance) {
		log.debug("deleting Project instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Project findById(java.lang.Integer id) {
		log.debug("getting Project instance with id: " + id);
		try {
			Project instance = (Project) getSession().get(
					"cn.math.appmath.entity.Project", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Project instance) {
		log.debug("finding Project instance by example");
		try {
			List results = getSession()
					.createCriteria("cn.math.appmath.entity.Project").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Project instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Project as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Project instances");
		try {
			String queryString = "from Project";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Project merge(Project detachedInstance) {
		log.debug("merging Project instance");
		try {
			Project result = (Project) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Project instance) {
		log.debug("attaching dirty Project instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Project instance) {
		log.debug("attaching clean Project instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}